<?php
require_once 'includes/haut.php';
require_once 'includes/donneesFormulaires.php';

$type = (isset($_POST["type"])) ? htmlentities($_POST["type"]) : NULL;
$niv = (isset($_POST["niv"])) ? htmlentities($_POST["niv"]) : NULL;

if($type && $niv) {
	
	if($type=='skiAlpin' || $type=='skiFond' || $type=='snow') $image="imgSki";
	else $image = "imgChauss";

	$req = materiel::getMaterielByTypeNiveau($type,$niv);
	
	if ($image == "imgChauss"){
		
		$count = $req->rowCount();
		$nbArticlesLigne = 3;
		if($count<$nbArticlesLigne) {
			$reste = $count;
// 			nb de lignes completes
			$quotient = 0;
		}
		else {
			$reste = $count % $nbArticlesLigne;
// 			nb de lignes completes
			$quotient = intval($count/$nbArticlesLigne);
		}
// 		ecriture des lignes completes
		for ($i=0;$i<$quotient;$i++) {
// 			on recupere les nbArticlesLigne requetes de la ligne
			for ($j=0;$j<$nbArticlesLigne;$j++) {
				$res[$j] = $req->fetch();
			}
			echo('<tr>');
// 			on remplit les nbArticlesLigne images
			for ($j=0;$j<$nbArticlesLigne;$j++) {
				echo('<td>');
					$src="images/materiel/".$res[$j]['marque']."_".$res[$j]['modele'].".png";
					if (file_exists($src)){
						echo('<img class="'.$image.'" src="'.$src.'" alt="Photo indisponible" title="'.$res[$j]['marque'].'_'.$res[$j]['modele'].'" />');
					}
					else echo "Photo indisponible";
				echo('</td>');
			}
			
			echo ('</tr><tr>');
			
// 			on remplit les nbArticlesLigne titres
			for ($j=0;$j<$nbArticlesLigne;$j++) {
				echo ('<th>'.$res[$j]['marque'].' '.$res[$j]['modele'].'</th>');
			}
				
			echo('</tr>');
			
		}
		
// 		ligne finale incomplete
		for ($j=0;$j<$reste;$j++) {
			$res[$j] = $req->fetch();
		}
		echo('<tr>');
		
		for ($j=0;$j<$reste;$j++) {
			echo('<td>');
			$src="images/materiel/".$res[$j]['marque']."_".$res[$j]['modele'].".png";
			if (file_exists($src)){
				echo('<img class="'.$image.'" src="'.$src.'" alt="Photo indisponible" title="'.$res[$j]['marque'].'_'.$res[$j]['modele'].'" />');
			}
			else echo "Photo indisponible";
			echo('</td>');
		}
			
		echo ('</tr><tr>');

		for ($j=0;$j<$reste;$j++) {
			echo ('<th>'.$res[$j]['marque'].' '.$res[$j]['modele'].'</th>');
		}

		echo('</tr>');
	}
	
// 	liste de skis
	else {
		while ($res = $req->fetch()){
			echo('<tr>
					<td>');
						$src="images/materiel/".$res['marque']."_".$res['modele'].".png";
						if (file_exists($src)){
							echo('<img class="'.$image.'" src="'.$src.'" alt="Photo indisponible" title="'.$res['marque'].'_'.$res['modele'].'" />');
						}
						else echo "Photo indisponible";
			echo('</td>
			      </tr><tr>
					<th>'
						.$res['marque'].' '.$res['modele'].
					'</th>
				</tr>');
		}
	}

	$req->closeCursor();
		
}
?>